package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dataStorage.DBData;


@ManagedBean(name="searchBean")
@SessionScoped
public class SearchBean {

	private String filter;
	private List<String> userList;
	
	
	private	HashMap<String, Boolean> searchList;
	private List<Boolean> isFriendList;
	private List<Integer> friendIDList;

	public SearchBean(){
		filter = "";
		userList = new ArrayList<String>();
		searchList = new HashMap<String, Boolean>();
		isFriendList = new ArrayList<Boolean>();
		friendIDList = new ArrayList<Integer>();
		counterIDIdx=0;
	}
	public synchronized String searchUser(){
		System.out.println(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("type"));
		
		String search = filter.toLowerCase();
		if (search.equals("")) search = ".*";
		
		Connection connection = null;
		try {
			connection = DBData.getConnection();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		String query = "SELECT UNAM, ID FROM ZUSER WHERE (UNAM REGEXP '" + search + "')";
		User u = (User)DBData.getSessionBean("userBean");
		int userID = u.getID();

		try {
			PreparedStatement ps = connection.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
	
				userList = new ArrayList<String>();
				isFriendList = new ArrayList<Boolean>();
				friendIDList = new ArrayList<Integer>();
				counterIDIdx=0;

				PreparedStatement pss;
				ResultSet rss;
				while (rs.next()){
					if (userID == rs.getInt(2)) continue;
					
					query = "SELECT CHID, OWID FROM ZFRIEND WHERE (OWID= " + userID + " AND CHID=" + rs.getInt(2) + ")";
					pss = connection.prepareStatement(query);
					rss = pss.executeQuery();
					if (!rss.next()){
						isFriendList.add(false);
					} else {
						isFriendList.add(true);
					}
					friendIDList.add(rs.getInt(2));
					userList.add(rs.getString(1));
				}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return null;
	}
	public String getFilter() {
		return filter;
	}
	public void setFilter(String filter) {
		this.filter = filter;
	}
	public List<String> getUserList() {
		return userList;
	}
	public void setUserList(ArrayList<String> userList) {
		this.userList = userList;
	}
	public List<Integer> getFriendIDList() {
		return friendIDList;
	}
	public void setFriendIDList(List<Integer> friendIDList) {
		this.friendIDList = friendIDList;
	}
	
	public List<Boolean> getIsFriendList() {
		return isFriendList;
	}
	public void setIsFriendList(List<Boolean> isFriendList) {
		this.isFriendList = isFriendList;
	}
	private int counterIDIdx;
	public String getNextId(){
		if (friendIDList.isEmpty()) return null;
		if (counterIDIdx >= friendIDList.size()) counterIDIdx=0;
		return String.valueOf((friendIDList.get(counterIDIdx++)));
	}
}
